package com.hypo.controller;

import com.hypo.annotation.LogOperation;
import com.hypo.pojo.Dept;
import com.hypo.pojo.Result;
import com.hypo.service.impl.DeptServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptServiceImpl deptService;

    /**
     * 修改
     */
    @LogOperation
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        return Result.success(deptService.getById(id));
    }

    @PutMapping
    public Result modify(@RequestBody Dept dept) {
        return Result.success(deptService.modify(dept));
    }

    /**
     * 新增
     *
     * @return 返回值
     */
    @PostMapping
    public Result add(@RequestBody Dept dept) {
        //if(1==1){
        //    return Result.error("cao");
        //}
        return Result.success(deptService.add(dept));
    }

    /**
     * 查询所有
     *
     * @return 返回结果
     */
    @GetMapping
    public Result findAll() {
        List<Dept> all = deptService.findAll();
        return Result.success(all);
    }

    /**
     * 根据Id删除
     */
    //方式一
    @DeleteMapping
    public Result del(Integer id) {
        return Result.success(deptService.del(id));
    }

    //方式二
    //@DeleteMapping("/depts")
    public Result delete(@RequestParam("id") Integer deptId) {
        //System.out.println("根据ID删除部门: " + deptId);
        return Result.success();
    }

    //方式三
    public Result delete(HttpServletRequest request) {
        String idStr = request.getParameter("id");
        int id = Integer.parseInt(idStr);

        //System.out.println("根据ID删除部门: " + id);
        return Result.success();
    }
}
